//
//  XMScrollViewVC.m
//  XiongMaoJiaSu
//
//  Created by 徐汉卿 on 16/5/31.
//  Copyright © 2016年 ISOYasser. All rights reserved.
//

#import "XMScrollViewVC.h"

@interface XMScrollViewVC ()<UIScrollViewDelegate>

@end


//static CGPoint lastContentOffset;
@implementation XMScrollViewVC

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear    %@   %f",self,self.sv.contentOffset.y);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianPanWillTanChu:) name:UIKeyboardWillShowNotification object:nil];
    
    
}

//-(void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    NSLog(@"viewWillAppear    %@   %f",self,self.sv.contentOffset.y);
//
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianPanWillTanChu:) name:UIKeyboardWillShowNotification object:nil];
//
//}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
     NSLog(@"viewWillDisappear    %@   %f",self,self.sv.contentOffset.y);
    [NSDC removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = kMainColorLightDarkGary;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jianPanWillTanChu:) name:UIKeyboardWillShowNotification object:nil];
    
//      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardFrameWillChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    [self buildUI];
}
//交给子类实现  防止子类没写崩溃,在此类写个空发发
-(void)buildUI{
    
}

-(UIScrollView *)sv
{
    if (_sv == nil) {
        _sv = [UIScrollView new];
        //解决 sv  再次出现 滚动位置变化
//        _sv.contentSize = CGSizeMake(0, 2 * kScreenHeight);
    

        _sv.scrollEnabled  = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shouJianPan)];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(shouJianPan)];
        [_sv addGestureRecognizer:tap];
         [_sv addGestureRecognizer:pan];
        
        
        [self.view addSubview:_sv];
        [_sv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _sv;
}




//-(void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//
//    _sv.bounces = YES;
//    [_sv setContentSize:CGSizeMake(0, kScreenHeight +200)];
//    
//}


-(void)shouJianPan
{
    
    [UIView animateWithDuration:0.2 animations:^{
        _sv.contentOffset  =CGPointZero;
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    }];

    
    
}


-(void)jianPanWillTanChu:(NSNotification *)aNotification{
    
    //坐标系转换
    
    //     NSLog(@"%f",kScreenHeight);
    //     NSLog(@"白色view在 SV的坐标在%@",NSStringFromCGRect(_viewWhite.frame));
    NSDictionary *userInfo    = [aNotification userInfo];
    NSValue *aValue           = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect       = [aValue CGRectValue];
    int KBHeight              = keyboardRect.size.height;
    
    
    CGRect rectNew =   [_bottomView.superview convertRect:_bottomView.frame toView:[UIApplication sharedApplication].keyWindow];
    CGFloat  yDiBu= rectNew.origin.y + rectNew.size.height;
    CGFloat  yDiBuMax =kScreenHeight - KBHeight;
    //      NSLog(@"yDiBu%f",yDiBu);
    //    NSLog(@"yDiBuMax%f",yDiBuMax);
    
    if (yDiBu > yDiBuMax) {
        //tbv 需要向上移动
        
        //         NSLog(@"-----动画之前----%@",NSStringFromCGPoint(_sv.contentOffset));
        
        
        CGFloat pY =  _sv.contentOffset.y;
        
         _sv.contentOffset = CGPointMake(0,  pY +(yDiBu - yDiBuMax));
//        [UIView animateWithDuration:0.25 animations:^{
//            _sv.contentOffset = CGPointMake(0,  pY +(yDiBu - yDiBuMax));
//        } completion:^(BOOL finished) {
//            //            NSLog(@"------动画之后---%@",NSStringFromCGPoint(_sv.contentOffset));
//        }];
        
        //        _sv.contentOffset = CGPointMake(0, 0);
    }
    
}



//- (void)keyBoardFrameWillChanged:(NSNotification *)note
//{
//    //获取键盘的frame
//    CGRect frame =  [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    
//    
//    //获取键盘的动画时间
//    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//    
//    //改变底部工具条的底部约束
//    //    self.toolBarBottomContraint.constant = self.view.frame.size.height  - frame.origin.y;
//    
//    [UIView animateWithDuration:duration animations:^{
//        self.view.y =  -(kScreenH - frame.origin.y);
//    }];
//    
//}






#pragma mark -- 代理

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//     NSLog(@"xxxxx%f",scrollView.contentOffset.y);
}


@end
